<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2013-08-19 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'mobiapi/modules/base.mod.php';
require ROOT_PATH."service/user.service.php";

class ModuleObj extends BaseModule
{
	private $service;
	
	function __construct($request)
	{
		//echo "3--- ".microtime()." ---\r\n";
		
		parent::__construct($request);
		
		$this->service = new UserService();
		
		$this->excute();
	}
	
	function excute()
	{
		switch($this->act)
		{
			case 'register':
				$this->register();
				break;
			case 'login':
				$this->login();
				break;
			case 'logout':
				$this->logout();
				break;
			case 'info':
				$this->getUserInfo();
				break;
			case 'update':
				$this->updateUser();
				break;
			case 'search':
				$this->searchUser();
				break;
			case 'addFollow':
				$this->addFollow();
				break;
			case 'removeFollow':
				$this->removeFollow();
				break;
			case 'followList':
				$this->getFollowList();
				break;
			case 'fansList':
				$this->getFansList();
				break;
			case 'recommend':
				$this->userRecommend();
				break;
			case 'feedback':
				$this->feedback();
				break;
			default:
				$this->message('ACTION_NOT_EXISTS', STATUS_E);
		}
		
		$this->showResponse();
	}
	
	function register()
	{
		$this->checkRequest('register');
		
		$user = $this->service->get($this->request['email']);
		if (check_array($user)) $this->message('USER_EXISTS', STATUS_E);
		
		$this->request['uid'] = rand(1, 99);
		$result = $this->service->register($this->request);
		
		if ($result)
		{
			$info = $this->service->get($result);
			unset($info['password']);
			unset($info['register_time']);
			unset($info['salt']);
			unset($info['user_id']);
		
			$this->user = $info;
			$this->updateSession();
			
			$this->response['status'] = STATUS_S;
			$this->response['info'] = $info;
		}
		else
		{
			$this->response['status'] = STATUS_F;
		}
	}
	
	function login()
	{
		//echo "4--- ".microtime()." ---\r\n";
		
		$this->checkRequest('login');
		
		//echo "5--- ".microtime()." ---\r\n";
		
		$result = $this->service->login($this->request);
		
		//echo "10--- ".microtime()." ---\r\n";
		
		if(!check_array($result)) $this->message('NOT_EXISTS', STATUS_F);
		
		unset($result['source_id']);
		unset($result['password']);
		unset($result['register_time']);
		unset($result['salt']);
		unset($result['user_id']);
		
		$this->user = $result;
		$this->updateSession();
		
		//echo "11--- ".microtime()." ---\r\n";
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $result;
	}
	
	function logout()
	{
		$_SESSION['user'] = null;
		
		$this->response['status'] = STATUS_S;
	}
	
	function getUserInfo()
	{		
		$uid = isset($this->request['id']) ? $this->request['id'] : $this->user['id'];
		
		$result = $this->service->get($uid, $this->user['id']);
		
		if(!check_array($result)) $this->message('NOT_EXISTS', STATUS_F);
		
		$this->response['info'] = $this->getResponse('user_info', $result);
	}
	
	function updateUser()
	{
		$this->checkRequest('user_info');
		
		if (isset($this->request['name']) && $this->request['name'] != '' && $this->request['name'] != $this->user['name'])
		{
			if (check_array($this->service->get($this->request['name']))) $this->message('NAME_EXISTS', STATUS_E);
		}
		
		$this->request['id'] = $this->user['id'];
		$this->request['uid'] = $this->user['id'];
		
		$result = $this->service->update($this->request);
		
		$this->response['status'] = ($result) ? STATUS_S : STATUS_F;
	}
	
	function searchUser()
	{
		$this->checkRequest('name_search');
		
		$result = $this->service->search($this->request);
		
		if(!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}
	
	function addFollow()
	{
		$this->checkRequest('info');
		
		if ($this->request['id'] == $this->user['id']) $this->message('SELF_FOLLOW', STATUS_E);
		
		$this->request['uid'] = $this->user['id'];
		
		$result = $this->service->add_follow($this->request);
		
		$this->response['status'] = ($result) ? STATUS_S : STATUS_F;
	}
	
	function removeFollow()
	{
		$this->checkRequest('info');
				
		$this->request['uid'] = $this->user['id'];
		
		$result = $this->service->remove_follow($this->request);
		
		$this->response['status'] = ($result) ? STATUS_S : STATUS_F;
	}
	
	function getFollowList()
	{
		//$this->checkRequest('info');
		
		if (!isset($this->request['id']) || $this->request['id'] == '') $this->request['id'] = $this->user['id'];
		
		$this->request['uid'] = $this->user['id'];
		$result = $this->service->get_follow_list($this->request);
		
		if(!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}
	
	function getFansList()
	{
		//$this->checkRequest('info');
		
		if (!isset($this->request['id']) || $this->request['id'] == '') $this->request['id'] = $this->user['id'];
		
		$this->request['uid'] = $this->user['id'];
		$result = $this->service->get_fans_list($this->request);
		
		if(!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}
	
	function userRecommend()
	{
		$this->request['uid'] = $this->user['id'];
		$result = $this->service->get_user_recommend($this->request);
		
		if(!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
	}
	
	function feedback()
	{
		$this->checkRequest('feedback');
		
		$this->request['uid'] = $this->user['id'];
		
		$result = $this->service->add_feedback($this->request);
		
		$this->response['status'] = ($result) ? STATUS_S : STATUS_F;
	}
}